﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PowerTriggers.ServiceReference
{
    internal enum HistoryItemTypes
    {
        Header,
        Detail,
    }

    internal enum HistoryItemImageTypes
    {
        None = 0,
        Informational = 1,
        Success = 2,
        Warning = 3,
        Error = 4,
    }


    internal interface IHistoryItem
    {
        string Key { get; }

        HistoryItemTypes Type { get; }

        HistoryItemImageTypes ImageType { get; }

        string LogFileText { get; }

        string ItemText { get; }
    }

    internal partial class HistoryHeader : IHistoryItem
    {
        public override string ToString()
        {
            return string.Format("{0}: {1}", this.DateTime.ToLocalTime().ToString(), this.Text);
        }

        public string Key
        {
            get
            {
                return string.Format("{0}.{1}.{2}.{3}"
                                        , this.Type
                                        , this.Severity
                                        , this.DateTime.ToString("O")
                                        , this.Text ?? string.Empty
                                        );
            }
        }

        public HistoryItemTypes Type
        {
            get { return HistoryItemTypes.Header; }
        }

        public string ItemText
        {
            get { return this.Text; }
        }

        public string LogFileText
        {
            get
            {
                string severityString = " ";
                if(this.Severity == HistorySeverityLevels.Warning || this.Severity == HistorySeverityLevels.Error)
                {
                    severityString = this.Severity.ToString().Substring(0,1);
                }
                return string.Format("{1}: {0} {2}", severityString, this.DateTime.ToLocalTime().ToString(), this.Text);
            }
        }

        public HistoryItemImageTypes ImageType
        {
            get
            {
                HistorySeverityLevels highestSeverity = this.Severity;
                foreach (var detail in this.Details)
                {
                    if ((int)detail.Severity > (int)highestSeverity)
                    {
                        highestSeverity = detail.Severity;
                    }
                }

                switch (highestSeverity)
                {
                    case HistorySeverityLevels.Informational:
                        return HistoryItemImageTypes.Informational;
                    case HistorySeverityLevels.Success:
                        return HistoryItemImageTypes.Success;
                    case HistorySeverityLevels.Warning:
                        return HistoryItemImageTypes.Warning;
                    case HistorySeverityLevels.Error:
                        return HistoryItemImageTypes.Error;
                    default:
                        return HistoryItemImageTypes.None;
                }
            }
        }
    }

    internal partial class HistoryDetail : IHistoryItem
    {
        public override string ToString()
        {
            return string.Format("{0}: {1}", this.DateTime.ToLocalTime().ToString(), this.Text);
        }

        public string Key
        {
            get
            {
                return string.Format("{0}.{1}.{2}.{3}"
                                        , this.Type
                                        , this.Severity
                                        , this.DateTime.ToString("O")
                                        , this.Text ?? string.Empty
                                        );
            }
        }

        public HistoryItemTypes Type
        {
            get { return HistoryItemTypes.Detail; }
        }

        public string ItemText
        {
            get { return this.Text; }
        }

        public string LogFileText
        {
            get
            {
                string severityString = " ";
                if (this.Severity == HistorySeverityLevels.Warning || this.Severity == HistorySeverityLevels.Error)
                {
                    severityString = this.Severity.ToString().Substring(0, 1);
                }
                return string.Format("{1}:   {0} {2}", severityString, this.DateTime.ToLocalTime().ToString(), this.Text);
            }
        }

        public HistoryItemImageTypes ImageType
        {
            get
            {
                switch (this.Severity)
                {
                    case HistorySeverityLevels.Success:
                        return HistoryItemImageTypes.Success;
                    case HistorySeverityLevels.Warning:
                        return HistoryItemImageTypes.Warning;
                    case HistorySeverityLevels.Error:
                        return HistoryItemImageTypes.Error;
                    default:
                        return HistoryItemImageTypes.None;
                }
            }
        }
    }

    internal partial class TaskPropertyChoice
    {
        public override string ToString()
        {
            return this.DisplayText;
        }
    }

    internal partial class Task
    {
        public TaskProperty GetAppliesToProperty()
        {
            return this.Properties.Where(p => p.Name == TaskPropertyNames.AppliesTo).FirstOrDefault();
        }

        public TaskProperty GetActionProperty()
        {
            return this.Properties.Where(p => p.Name == TaskPropertyNames.Action).FirstOrDefault();
        }
    }

    internal static class EnumUtilities
    {
        internal static string ConvertEnumToText(object enumValue)
        {
            StringBuilder sb = new StringBuilder();
            foreach (char ch in enumValue.ToString())
            {
                if (Char.IsUpper(ch) && sb.Length > 0)
                {
                    sb.Append(" ");
                }
                sb.Append(ch);
            }

            return sb.ToString();
        }

        internal static string ConvertTextToEnum(string text)
        {
            return text.Replace(" ", string.Empty);
        }

        internal static T ConvertTextToEnum<T>(string text)
        {
            return (T)Enum.Parse(typeof(T), text.Replace(" ", string.Empty), true);
        }
    }
}
